1105D - Kilani and the Game - CodeForces Solution


dfs and similar graphs implementation shortest paths *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define F first
#define S second
#define endl "\n";
#define shekoo ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

const int N = 1e3+5, M = 12;
char arr[N][N];
int expansion[M], ans[M], n, m, p, cnt[M];
queue<pair<pi, int>> g[M];
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};


bool valid(int x, int y){
    return x < n and x >= 0 and y < m and y >= 0 and arr[x][y] == '.';
}

void bfs(int playernum){
    cnt[playernum]++;
    while(!g[playernum].empty()){
        int x = g[playernum].front().F.F, y = g[playernum].front().F.S, step = g[playernum].front().S;
        if(step == (expansion[playernum]*cnt[playernum])) return;
        for(int i=0; i<4; i++){
            int xx = x + d[i][0], yy = y + d[i][1];
            if(valid(xx, yy)){
                arr[xx][yy] = char('0'+playernum);
                g[playernum].push({{xx, yy}, step+1});
            }
        }
        g[playernum].pop();
    }
}

void solve(){
    for(int i=1; i<=p; i++) bfs(i);
    bool f = 0;
    for(int i=1; i<=p; i++){
        if(!g[i].empty()){
            f = 1;
            break;
        }
    }
    if(f) solve();
    else{
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
//                cout << arr[i][j];
                if(arr[i][j] == '.' or arr[i][j] == '#') continue;
                ans[arr[i][j]-'0']++;
            }
//            cout << endl
        }
        for(int i=1; i<=p; i++) cout << ans[i] << " ";
    }
}

int main() {
    shekoo
    cin >> n >> m >> p;
    for(int i=1; i<=p; i++) cin >> expansion[i];
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin >> arr[i][j];
            if(arr[i][j] == '.' or arr[i][j] == '#') continue;
            g[(arr[i][j]-'0')].push({{i, j}, 0});
        }
    }
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie